<template>
    <mescroll-uni top="20" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption"  class="message" @init="mescrollInit">
        <view  class="common-interval">
            <gw-book-card
                    v-for="(item, index) in bookList"
                    :key="index"
                    :item="item"
                    @linkTo="linkTo"
                    @updateBooks="updateBooks"
            />
        </view>
    </mescroll-uni>
</template>

<script>
    import gwBookCard from '@/pages/components/gw-book-card';
    import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue";
    import { getBooks } from '@/util/commonFn';
    export default {
        name: "collection",
        components: {
            gwBookCard, MescrollUni
        },
        data(){
            return{
                bookList: [],
                orderId: '',
                page: 1,
                limit: 10,
                downOption: {
                    auto: true,
                },
                upOption:{
                    use: true, // 是否启用上拉加载; 默认true
                    auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
                    page: {
                        num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
                        size: 10 // 每页数据的数量,默认10
                    },
                    noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
                    empty: {
                        icon: 'https://img.thumbrary.com/empty.png',
                        tip: '暂无数据'
                    },
                    offset: 130,
                    textNoMore:'我是有底线的 >_<'
                },
                mescroll: null
            }
        },
        methods: {
            mescrollInit(mescroll) {
                this.mescroll = mescroll;
            },
            downCallback(mescroll){
                this.bookList = [];
                this.page = 1;
                mescroll.resetUpScroll();
            },
            upCallback(mescroll){
                this.page = mescroll.num;
                this.getBooks();
            },
            linkTo(item) {
                uni.navigateTo({
                    url: `../../pages/components/gw-book-detail?booksId=${item.booksId}`
                })
            },
            updateBooks(){
                this.getBooks({
                    orderId: this.orderId,
                    page: 1,
                    limit: this.bookList.length,
                });
            },
            getBooks (para) {
                let params = para || {
                    orderId: this.orderId,
                    page: this.page,
                    limit: this.limit,
                };
                getBooks(params).then(result=>{
                    let data = result.data || [];
                    (this.page > 1 && !para) ? this.bookList.push(...data) : this.bookList = data;
                    this.mescroll && this.mescroll.endBySize(data.length, data.num);
                });
            },
        },
        onLoad(params){
            let { orderId } = params;
            this.orderId = orderId;
            // this.getBooks();
        },
    }
</script>

<style scoped>

</style>